
#include <BStyle>
#include <BLabel>
#include <BGridLayout>
#include <BHBoxLayout>
#include <BVBoxLayout>
#include <BFileDialog>
#include <BColorDialog>
#include <BReadWrite>

#include "CanvasWidget.h"

CanvasWidget::CanvasWidget()
{
	_pixmapArea = 0;
	
	BLabel* formatLabel = new BLabel("Format : rgba");

	BGridLayout* gridLayout = new BGridLayout();
	{
		BLabel* widthLabel = new BLabel("Width");
		_canvasWidthSpinBox = new BSpinBox();
		_canvasWidthSpinBox->setSuffix("px");
		_canvasWidthSpinBox->setFormulable(true);

		BLabel* heightLabel = new BLabel("Height");
		_canvasHeightSpinBox = new BSpinBox();
		_canvasHeightSpinBox->setSuffix("px");
		_canvasHeightSpinBox->setFormulable(true);

		gridLayout->addWidget(1, 0, widthLabel);
		gridLayout->addWidget(1, 1, _canvasWidthSpinBox);
		gridLayout->addWidget(2, 0, heightLabel);
		gridLayout->addWidget(2, 1, _canvasHeightSpinBox);
	}

	BHBoxLayout* backgroundLayout = new BHBoxLayout();
	{
		BLabel* backgroundLabel = new BLabel("background");
		_canvasImageButton = new BButton();
		_clearImageButton = new BButton("clear");
		_clearImageButton->setForm(Form_IconAlone);
		backgroundLayout->addWidget(backgroundLabel, 0);
		backgroundLayout->addWidget(_canvasImageButton);
		backgroundLayout->addWidget(_clearImageButton, 0);
	}

	BHBoxLayout* chessGridLayout = new BHBoxLayout();
	{
		_chessGridCheckBox = new BCheckBox("chess-grid");
		_gridDarknessSpinBox = new BSpinBox(1, 200);
		chessGridLayout->addWidget(_chessGridCheckBox, 0);
		chessGridLayout->addWidget(_gridDarknessSpinBox);
	}

	BVBoxLayout* vlayout = new BVBoxLayout(this);
	vlayout->addWidget(formatLabel);
	vlayout->addLayout(gridLayout);
	vlayout->addLayout(backgroundLayout);
	vlayout->addLayout(chessGridLayout);

	connect(this, Signal_AssetChanged, &CanvasWidget::slotAssetChanged);

	connect(_canvasWidthSpinBox, Signal_EditFinished, &CanvasWidget::slotCanvasResized);
	connect(_canvasHeightSpinBox, Signal_EditFinished, &CanvasWidget::slotCanvasResized);
	connect(_canvasImageButton, Signal_Clicked, &CanvasWidget::slotChooseCanvasImageButtonClicked);
	connect(_clearImageButton, Signal_Clicked, &CanvasWidget::slotClearCanvasImageButtonClicked);
	connect(_chessGridCheckBox, Signal_Toggled, &CanvasWidget::slotChessGridCheckBoxToggled);
	connect(_gridDarknessSpinBox, Signal_ValueEdited, &CanvasWidget::slotGridDarknessEdited);

	_canvasWidthSpinBox->setValue(100);
	_canvasHeightSpinBox->setValue(100);
	_chessGridCheckBox->setChecked(true);
	_gridDarknessSpinBox->setValue(100);

}
CanvasWidget::~CanvasWidget()
{

}

void CanvasWidget::slotAssetChanged(BObject* object, const BValue& value)
{
	PixmapArea* pixmapArea = value;
	if (_pixmapArea != pixmapArea)
	{
		if (_pixmapArea)
			disconnect(_pixmapArea);
		_pixmapArea = pixmapArea;
		if (_pixmapArea)
		{
			BPixmap* pixmap = _pixmapArea->pixmap();
			_canvasWidthSpinBox->setValue(pixmap->width());
			_canvasHeightSpinBox->setValue(pixmap->height());
			connect(pixmap, Signal_Resized, &CanvasWidget::slotPixmapSizeChanged);
		}
	}
}

void CanvasWidget::slotCanvasResized(BObject* object, const BValue& value)
{
	int width = _canvasWidthSpinBox->value();
	int height = _canvasHeightSpinBox->value();
	_pixmapArea->pixmap()->setSize(width, height);
}
void CanvasWidget::slotPixmapSizeChanged(BPixmap* pixmap, const BValue& value)
{
	_canvasWidthSpinBox->setValue(pixmap->width());
	_canvasHeightSpinBox->setValue(pixmap->height());
}
void CanvasWidget::slotChooseCanvasImageButtonClicked(BObject* object, const BValue& value)
{
	BButton* button = dynamic_cast<BButton*>(object);
	const BString& fileName = BFileDialog::OpenFile("Choose canvas image", "", "*.png;*.jpg;*.bmp;*.tiff");
	if (fileName.size())
	{
		BImage* image = ReadFile<BImage>(fileName);
		button->setImage(image);
		_pixmapArea->setAreaImage(image);
	}
}
void CanvasWidget::slotClearCanvasImageButtonClicked(BObject* object, const BValue& value)
{
	_pixmapArea->setAreaImage(0);
}
void CanvasWidget::slotChessGridCheckBoxToggled(BObject* object, const BValue& value)
{
	bool checked = value;
	if (checked)
	{
		_pixmapArea->setGridDrakness(_gridDarknessSpinBox->value());
		_gridDarknessSpinBox->setEnabled(true);
	}
	else
	{
		_pixmapArea->setGridDrakness(0);
		_gridDarknessSpinBox->setEnabled(false);
	}
}
void CanvasWidget::slotGridDarknessEdited(BObject* object, const BValue& value)
{
	int darkness = value;
	_pixmapArea->setGridDrakness(darkness);
}

void CanvasWidget::styleEvent(const BEvent& event)
{
	BWidget::styleEvent(event);
	const BStyle* style = event.value();
	_clearImageButton->setIcon(style->icon("clear"));
	_canvasImageButton->setTips(style->text("tips-canvas-image"));
	_clearImageButton->setTips(style->text("tips-clear-image"));
	_chessGridCheckBox->setTips(style->text("tips-chess-grid"));
	_gridDarknessSpinBox->setTips(style->text("tips-grid-darkness"));
}